﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/5/22 星期三 22:22:24
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sector7.Medical.Consts;
using Sector7.Framework.Core;

namespace Sector7.Medical.Utility
{
    /// <summary>
    /// 主键生成器
    /// </summary>
    public class PrimaryKeyUtil
    {
        #region Public functions
        /// <summary>
        /// 获取主键编号
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static object GetPrimaryKey(string tableName, params object[] args)
        {
            object primaryKey = null;

            switch (tableName)
            {
                case C_M7_TableName.DRUG_TYPE:
                    primaryKey = GenerateDrugTypePrimaryKey(args);
                    break;
                case C_M7_TableName.DRUG:
                    primaryKey = GenerateDrugPrimaryKey(args);
                    break;
                case C_M7_TableName.PURCHASES_BILL:
                case C_M7_TableName.RECIPE:
                case C_M7_TableName.MEDICAL_RECORD:
                case C_M7_TableName.ADJUST_RECORD:
                    primaryKey = GenerateBillPrimaryKey(args);
                    break;
                case C_M7_TableName.HEALTH_ARCHIVE:
                    primaryKey = GenerateHealthArchivePrimaryKey();
                    break;
                case C_M7_TableName.EMPLOYEE:
                    primaryKey = GenerateEmployeePrimaryKey(args);
                    break;
                case C_M7_TableName.PATIENT:
                    primaryKey = GeneratePatientPrimaryKey(args);
                    break;
                case C_M7_TableName.REGISTRY_RECORD:
                    primaryKey = GenerateRegistryRecordPrimaryKey(args);
                    break;
                case C_M7_TableName.DISPENSE_BILL:
                    primaryKey = GenerateDispenseBillPrimaryKey(args);
                    break;
                default:
                    break;
            }

            return primaryKey;
        }
        #endregion

        #region Private functions
        private static object GenerateHealthArchivePrimaryKey(params object[] args)
        {
            object primaryKey = null;

            if (args.Length == 2)
            {
                primaryKey = string.Format("{0}{1}{2:00000}",
                    Senvironments.SystemAdmDivisionId,  // 终端所在行政区划代码
                    args[0],    // 建档日期
                    args[1]);   // 流水号
            }

            return primaryKey;
        }

        private static object GenerateEmployeePrimaryKey(params object[] args)
        {
            object primaryKey = null;

            if (args.Length == 3)
            {
                primaryKey = string.Format("{0:000}{1:0000}{2:00000}",
                    args[0],    // 入职年份
                    args[1],    // 所属科室
                    args[2]);   // 流水号
            }

            return primaryKey;
        }

        private static object GenerateDrugTypePrimaryKey(params object[] args)
        {
            object primaryKey = null;

            if (args.Length == 1)
            {
                primaryKey = args[0];
            }
            else if (args.Length == 2)
            {
                primaryKey = string.Format("{0}{1:00}",
                    args[0],
                    args[1]);
            }

            return primaryKey;
        }

        private static object GenerateDrugPrimaryKey(params object[] args)
        {
            object primaryKey = null;

            if (args.Length == 2)
            {
                primaryKey = string.Format("{0}00{1:00000}",
                    args[0],
                    args[1]);
            }

            return primaryKey;
        }

        private static object GenerateBillPrimaryKey(params object[] args)
        {
            object primaryKey = null;

            if (args.Length == 3)
            {
                primaryKey = string.Format("{0}{1:000}{2:000000}",
                    args[0],    // 录入日期
                    args[1],    // 单据类型
                    args[2]);   // 流水号
            }

            return primaryKey;
        }

        private static object GeneratePatientPrimaryKey(params object[] args)
        {
            object primaryKey = null;

            if (args.Length == 3)
            {
                primaryKey = string.Format("{0:000000}{1:000000}{2:000000}",
                    args[0],    // 行政区编码
                    args[1],    // 建档日期
                    args[2]);   // 流水号
            }

            return primaryKey;
        }

        private static object GenerateRegistryRecordPrimaryKey(params object[] args)
        {
            object primaryKey = null;

            if (args.Length == 3)
            {
                primaryKey = string.Format("{0:000000}{1:000}{2:000000}",
                    args[0],    // 建档日期
                    args[1],    // 单据类型
                    args[2]);   // 流水号
            }

            return primaryKey;
        }

        private static object GenerateDispenseBillPrimaryKey(params object[] args)
        {
            object primaryKey = null;

            if (args.Length == 3)
            {
                primaryKey = string.Format("{0:00000000}{1:000}{2:000000}",
                    args[0],    // 建档日期
                    args[1],    // 单据类型
                    args[2]);   // 流水号
            }

            return primaryKey;
        }
        
        #endregion
    }
}
